package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle

import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bp.hearthealth.pulse.lnapps.base.BpBaseViewModel
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle.bp_bean.BpLifeStyleBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

/**
 * @author isnoam
 * @package_name LifeStyleViewModel.kt
 * @description：
 */
class BpLifeStyleViewModel(private val repository: BpLifeStyleRepository) : BpBaseViewModel() {

    val bloodPerssureList = MutableLiveData<MutableList<BpLifeStyleBean>>()
    val bloodSugarList = MutableLiveData<MutableList<BpLifeStyleBean>>()
    val healthyDietList = MutableLiveData<MutableList<BpLifeStyleBean>>()
    val allList = MutableLiveData<MutableList<MutableList<BpLifeStyleBean>>>()
    var selectedPosition = MutableLiveData(-1)
    var selected = ObservableField<Boolean>()

    fun refreshTab(position: Int) {
        if (selectedPosition.value != position) {
            selectedPosition.value = position
        }
    }

    fun loadData() {
        viewModelScope.launch(Dispatchers.Main) {
            val bloodPressureDeferred = async { repository.loadBloodPerssure() }
            val bloodSugarDeferred = async { repository.loadBloodSugar() }
            val healthyDietDeferred = async { repository.loadHealthyDiet() }
            val loadBloodPerssure = bloodPressureDeferred.await()
            val loadBloodSugar = bloodSugarDeferred.await()
            val loadHealthyDiet = healthyDietDeferred.await()
            val list = mutableListOf(
                /*mutableListOf(),*/ loadBloodPerssure, loadBloodSugar, loadHealthyDiet
            )
            allList.value = list
        }
    }

    fun loadBloodPerssure() {
        viewModelScope.launch(Dispatchers.Main) {
            bloodPerssureList.value = repository.loadBloodPerssure()
        }
    }

    fun loadBloodSugar() {
        viewModelScope.launch(Dispatchers.Main) {
            bloodSugarList.value = repository.loadBloodSugar()
        }
    }

    fun loadHealthyDiet() {
        viewModelScope.launch(Dispatchers.Main) {
            healthyDietList.value = repository.loadHealthyDiet()
        }
    }
}